//
//  DetailViewController.m
//  Presidents
//
//  Created by Justsoso on 16/3/10.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "DetailViewController.h"
#import "LanguageViewController.h"

@interface DetailViewController ()
@property (weak, nonatomic) IBOutlet UILabel *urlLabel;
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@property (nonatomic,strong) UIPopoverController *popVC;



@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    LanguageViewController *lVC = [[LanguageViewController alloc] init];
    lVC.detaiVC = self;
    self.popVC = [[UIPopoverController alloc] initWithContentViewController:lVC];
    self.popVC.popoverContentSize = CGSizeMake(320, 44*4);
    
    
}

- (IBAction)popBtnPressed:(id)sender
{
    if (self.popVC.popoverVisible)
    {
        [self.popVC dismissPopoverAnimated:YES];
    }
    else
    {
        [self.popVC presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
}


- (void)setUrl:(NSString *)url
{
    _url = url;
    
    [self loadWeb];
    
}
- (void)setLanguage:(NSString *)language
{
    _language = language;
    [self.popVC dismissPopoverAnimated:YES];
    
    [self loadWeb];
}

- (void)loadWeb
{
    NSString *tmpUrl = self.url;
    NSString *tmpLanguage = self.language;
    
    if (tmpUrl != nil)
    {
        if (tmpLanguage != nil)
        {
            NSRange range = NSMakeRange(7, 2);
            tmpUrl = [tmpUrl stringByReplacingCharactersInRange:range withString:self.language];
        }
        self.urlLabel.text = tmpUrl;
        
        NSURL *url = [NSURL URLWithString:tmpUrl];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [self.webView loadRequest:request];
    }
}

@end
